

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;

/**
 * Servlet implementation class view
 */
public class view extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public view() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/xml");
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		HttpSession Session=request.getSession();
		setId_query(new Integer(request.getParameter("id")), Session);
		setRule(request.getParameter("rule"), Session);
		ResultSet rs = null;
		Statement st = null;
		Connection cn = null;
		DataSource ds = null;
		PrintWriter out= response.getWriter();
//		String answer="<?xml version=\"1.0\" encoding=\"UTF-8\"?><rows>";
		out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?><rows>");
		
		try{
			InitialContext initCtx = new InitialContext();
			ds=(DataSource)initCtx.lookup("java:comp/env/jdbc/newDs");
			cn=ds.getConnection();
			st=cn.createStatement();
			String SQL="";
			switch(getId_query(Session)){
			case(0):   //Выбор менеджеров id=0 rule=тип документа
			{
				SQL=String.format("select trim(name) from manager where id_rules in (select id_rules from rules where id_doc like '%s') order by upper(trim(name))", "%;"+getRule(Session)+";%");
			}
			}
			rs=st.executeQuery(SQL);
			while (rs.next()){
				out.println(String.format("<row><name>%s</name></row>", rs.getString(1)));
//				out.print("</name></row>");
//				answer=answer+String.format("<row><name>%s\n</name></row>", rs.getString(1));
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		out.println("</rows>");
		try {
			rs.close();
			st.close();
			cn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

			
	}
	private Integer getId_query(HttpSession session){
		if (session!=null)
			return (Integer)session.getAttribute("id");
		else
			return null;
	}
	private void setId_query(int value, HttpSession session){
		if (session!=null)
			session.setAttribute("id", value);
	}

	private String getRule(HttpSession session){
		if (session!=null)
			return (String)session.getAttribute("rule");
		else
			return "";
	}
	private void setRule(String value, HttpSession session){
		if (session!=null)
			session.setAttribute("rule", value);
	}

	/**
	
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */

}
